package icegps.com.pingdj.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;
import icegps.com.pingdj.R;
import icegps.com.pingdj.bean.PickerBean;
import icegps.com.pingdj.listener.OnUnitIndexListener;

/**
 * Created by 111 on 2018/4/3.
 */

public class DialogPicker extends Dialog {
    public static final int PICKER_VIEW = 1;
    public static final int TEXT_VIEW = 2;
    private LinearLayout rootView;
    private final View view;
    private NumberPickerView numberPickerView;
    private int viewId;
    private ArrayList<View> views;

    public DialogPicker(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        view = View.inflate(getContext(), R.layout.dialog_picker, null);
        rootView = view.findViewById(R.id.root_view);
        views = new ArrayList<>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation); // 添加动画
        WindowManager.LayoutParams lp = window.getAttributes(); // 获取对话框当前的参数值
        lp.width = getContext().getResources().getDisplayMetrics().widthPixels; // 宽度
        window.setAttributes(lp);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                int[] selIndexs = new int[views.size()];
                String[] selValues = new String[views.size()];

                if (onUnitIndexListener != null && numberPickerView != null) {
                    for (int i = 0; i < views.size(); i++) {
                        int type = (int) views.get(i).getTag();
                        switch (type) {
                            case PICKER_VIEW:
                                NumberPickerView numberPickerView = (NumberPickerView) views.get(i);
                                selValues[i] = numberPickerView.getContentByCurrValue();
                                selIndexs[i] = numberPickerView.getValue();
                                break;
                            case TEXT_VIEW:
                                TextView tv = (TextView) views.get(i);
                                String s = tv.getText().toString();
                                String matches = "[0-9]*\\.?\\+?\\-?";
                                if (s.matches(matches)) {
                                    selValues[i] = s;
                                } else {
                                    selValues[i] = "0";
                                }
                                selIndexs[i] = 0;
                                break;
                        }
                    }

                    onUnitIndexListener.onUnitIndex(viewId, selIndexs, selValues);
                }
            }
        });
    }

    public void setLayoutOrder(ArrayList<PickerBean> pickerBeans, int viewId) {
        rootView.removeAllViews();
        views.clear();
        this.viewId = viewId;
        for (PickerBean pickerBean : pickerBeans) {
            switch (pickerBean.getType()) {
                case PICKER_VIEW:
                    numberPickerView = getNumberPickerView(pickerBean.getValues(), pickerBean.getIndex());
                    numberPickerView.setTag(PICKER_VIEW);
                    rootView.addView(numberPickerView);
                    views.add(numberPickerView);
                    break;
                case TEXT_VIEW:
                    TextView tv = getTextView(pickerBean.getValues()[0]);
                    tv.setTag(TEXT_VIEW);
                    rootView.addView(tv);
                    views.add(tv);
                    break;
            }
        }
        setContentView(view);
    }


    private NumberPickerView getNumberPickerView(String[] values, int index) {
        NumberPickerView numberPickerView = (NumberPickerView) View.inflate(getContext(), R.layout.numberpickerview, null);
        numberPickerView.setWrapSelectorWheel(false);
        numberPickerView.setDisplayedValuesAndPickedIndex(values, index, false);
        return numberPickerView;
    }

    private TextView getTextView(String str) {
        TextView tv = (TextView) View.inflate(getContext(), R.layout.picker_tv, null);
        tv.setText(str);
        return tv;
    }

    OnUnitIndexListener onUnitIndexListener;

    public void setOnUnitIndexListener(OnUnitIndexListener onUnitIndexListener) {
        this.onUnitIndexListener = onUnitIndexListener;
    }
}
